package bTree.LeetCode;

public class 最大二叉树 {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        int start=0;
        int end=nums.length-1;
        return construct(nums,start,end);
    }

    private TreeNode construct(int[] nums, int start, int end) {
        if (start>end){
            return null;
        }
        //找出最大值
        int max=nums[start];
        int index=start;
        for (int i=start;i<=end;i++){
            if (nums[i]>max){
                max=nums[i];
                index=i;
            }
        }
        TreeNode maxNode=new TreeNode(max);
        maxNode.left=construct(nums,start ,index-1 );
        maxNode.right=construct(nums,index+1 ,end );
        return maxNode;
    }
}